//
//  SEGMissionRecordListViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/19.
//

#import "SEGMissionRecordListViewModel.h"

@implementation SEGMissionRecordListViewModel
#pragma mark - life cycle
- (instancetype)init {
    if (self = [super init]) {
        [self seg_creatListCommand];
    }
    return self;
}

#pragma mark - init

#pragma mark - send request
- (void)seg_creatListCommand {
    @weakify(self);
    self.listCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSNumber * input) {
        @strongify(self);
        if (input.boolValue) {
            self.pageNo = 1;
        }
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            __weak typeof (self) weakSelf = self;
            [[SEGWinningrecordlistManager sharedManager] requestWinningrecordlistWithPageno:[@(self.pageNo) stringValue] pageLimit:@"15" finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                //添加刷新控件 步骤4：停止动画
                [subscriber sendNext:nil];

                if (isSuccessed) {
                    weakSelf.pageNo += 1;//数据加载成功后加pageNo
                    // 显示到界面上
                    [subscriber sendCompleted];
                }else {
                    [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                }
            }];
            return nil;
        }];
    }];
}
#pragma mark - custom delegate

#pragma mark - system delegate

#pragma mark - event response

#pragma mark - private methods

#pragma mark - setter and getter
- (SEGActivityManager *)activityManager {
    if (!_activityManager) {
        _activityManager = [[SEGActivityManager alloc] init];
    }
    return _activityManager;
}

@end
